package com.gyg.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.gyg.domain.*;
import com.gyg.dao.CloudCollectionMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.utils.TimeUtilCustomize;
import com.gyg.vo.param.CollectionParam;
import com.gyg.vo.param.UpdateCollectionParam;
import com.gyg.vo.param.UpdateFileParam;
import com.gyg.vo.result.CollectionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件收藏表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
@Slf4j
public class CloudCollectionServiceImpl extends ServiceImpl<CloudCollectionMapper, CloudCollection> implements CloudCollectionService {

    @Autowired
    private CloudPictureService cloudPictureService;

    @Autowired
    private CloudDocumentService cloudDocumentService;

    @Autowired
    private CloudAudioService cloudAudioService;

    @Autowired
    private CloudVideoService cloudVideoService;

    @Override
    public RespEntity fileAddCollection(CollectionParam collectionParam) {

        if (Objects.isNull(collectionParam)) {
            log.info("参数非法或格式不正确！");
            return new RespEntity(RespCode.PARAMETERS_ERROR);
        }

        //校验文件是否已被收藏
        QueryWrapper<CloudCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudCollection::getUserId, collectionParam.getUserId())
                .eq(CloudCollection::getFileId, collectionParam.getFileId())
                .eq(CloudCollection::getFileType, collectionParam.getFileType())
                .eq(CloudCollection::getDeleted, 0);
        CloudCollection collection = baseMapper.selectOne(queryWrapper);

        if (Objects.nonNull(collection)) {
            log.info("该文件已经收藏过了");
            return new RespEntity(RespCode.EXIST_COLLECTION);
        }

        //当前时间
        String fileTime = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, new Date());
        CloudCollection cloudCollection = CloudCollection.builder()
                .userId(collectionParam.getUserId())
                .fileId(collectionParam.getFileId())
                .fileType(collectionParam.getFileType())
                .fileTime(fileTime)
                .build();

        //添加收藏
        baseMapper.insert(cloudCollection);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity<CollectionResult> collectionList(Integer userId) {
        QueryWrapper<CloudCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudCollection::getUserId, userId)
                .eq(CloudCollection::getDeleted, 0)
                .orderByDesc(CloudCollection::getId);
        List<CloudCollection> cloudCollectionList = baseMapper.selectList(queryWrapper);

        if (Objects.isNull(cloudCollectionList)) {
            return new RespEntity<>(RespCode.SUCCESS, new CollectionResult());
        }

        //图片收藏列表
        List<CloudCollection> pictureCollectionList = cloudCollectionList.stream()
                .filter(v -> v.getFileType().equals(1))
                .collect(Collectors.toList());

        //文档收藏列表
        List<CloudCollection> documentCollectionList = cloudCollectionList.stream()
                .filter(v -> v.getFileType().equals(2))
                .collect(Collectors.toList());

        //音频收藏列表
        List<CloudCollection> audioCollectionList = cloudCollectionList.stream()
                .filter(v -> v.getFileType().equals(3))
                .collect(Collectors.toList());

        //视频收藏列表
        List<CloudCollection> videoCollectionList = cloudCollectionList.stream()
                .filter(v -> v.getFileType().equals(4))
                .collect(Collectors.toList());

        //图片详情列表
        List<CloudPicture> cloudPictureList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(pictureCollectionList)) {
            for (CloudCollection pictureCollection : pictureCollectionList) {
                CloudPicture cloudPicture = cloudPictureService.getById(pictureCollection.getFileId());
                cloudPicture.setPictureTime(pictureCollection.getFileTime());
                cloudPictureList.add(cloudPicture);
            }
        }

        //文档详情列表
        List<CloudDocument> cloudDocumentList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(documentCollectionList)) {
            for (CloudCollection documentCollection : documentCollectionList) {
                CloudDocument cloudDocument = cloudDocumentService.getById(documentCollection.getFileId());
                cloudDocument.setDocumentTime(documentCollection.getFileTime());
                cloudDocumentList.add(cloudDocument);
            }
        }

        //音频详情列表
        List<CloudAudio> cloudAudioList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(audioCollectionList)) {
            for (CloudCollection audioCollection : audioCollectionList) {
                CloudAudio cloudAudio = cloudAudioService.getById(audioCollection.getFileId());
                cloudAudio.setAudioTime(audioCollection.getFileTime());
                cloudAudioList.add(cloudAudio);
            }
        }

        //视频详情列表
        List<CloudVideo> cloudVideoList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(videoCollectionList)) {
            for (CloudCollection videoCollection : videoCollectionList) {
                CloudVideo cloudVideo = cloudVideoService.getById(videoCollection.getFileId());
                cloudVideo.setVideoTime(videoCollection.getFileTime());
                cloudVideoList.add(cloudVideo);
            }
        }

        return new RespEntity<>(RespCode.SUCCESS,
                new CollectionResult(cloudPictureList, cloudDocumentList, cloudAudioList, cloudVideoList));
    }

    @Override
    public RespEntity updateCollection(UpdateCollectionParam updateCollectionParam) {
        Integer fileType = updateCollectionParam.getFileType();
        UpdateFileParam updateFileParam = new UpdateFileParam(updateCollectionParam.getFileId(),
                updateCollectionParam.getFileName());

        switch (fileType) {
            case 1 : cloudPictureService.editPicture(updateFileParam); break;
            case 2 : cloudDocumentService.editDocument(updateFileParam); break;
            case 3 : cloudAudioService.editAudio(updateFileParam); break;
            case 4 : cloudVideoService.editVideo(updateFileParam); break;
        }

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity cancelCollection(CollectionParam collectionParam) {
        QueryWrapper<CloudCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudCollection::getUserId, collectionParam.getUserId())
                .eq(CloudCollection::getFileId, collectionParam.getFileId())
                .eq(CloudCollection::getFileType, collectionParam.getFileType())
                .eq(CloudCollection::getDeleted, 0);
        CloudCollection cloudCollection = baseMapper.selectOne(queryWrapper);

        if (Objects.isNull(cloudCollection)) {
            return null;
        }

        //取消收藏
        cloudCollection.setDeleted(1);
        baseMapper.updateById(cloudCollection);

        return new RespEntity(RespCode.SUCCESS);
    }
}
